

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pyamp.patterns.interfaces.interface &mdash; pyamp v1.2 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="top" title="pyamp v1.2 documentation" href="../../../../index.html" />
    <link rel="up" title="pyamp.patterns" href="../../patterns.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../../patterns.html" accesskey="U">pyamp.patterns</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for pyamp.patterns.interfaces.interface</h1><div class="highlight"><pre>
<span class="c"># Copyright 2012 Brett Ponsler</span>
<span class="c"># This file is part of pyamp.</span>
<span class="c">#</span>
<span class="c"># pyamp is free software: you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU General Public License as published by</span>
<span class="c"># the Free Software Foundation, either version 3 of the License, or</span>
<span class="c"># (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># pyamp is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c"># GNU General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU General Public License</span>
<span class="c"># along with pyamp.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="sd">&#39;&#39;&#39;The interface module provides a base class which provides a function to</span>
<span class="sd">decorate functions in order to label specific functions as *interface*</span>
<span class="sd">functions.</span>

<span class="sd">The :class:`PassThrough&#39; class is an implementation of the :class:`Interface`</span>
<span class="sd">class which provides the ability to connect a child :class:`PassThrough` object</span>
<span class="sd">to a parent :class:`PassThrough` object so that any time the child&#39;s interface</span>
<span class="sd">functions are called, the call is instead redirected to the parent&#39;s function</span>
<span class="sd">of the same name.</span>

<span class="sd">&#39;&#39;&#39;</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;Interface&quot;</span><span class="p">,</span> <span class="s">&quot;PassThrough&quot;</span><span class="p">]</span>


<div class="viewcode-block" id="Interface"><a class="viewcode-back" href="../../../../_generated/pyamp_patterns_interfaces_interface.html#pyamp.patterns.interfaces.interface.Interface">[docs]</a><span class="k">class</span> <span class="nc">Interface</span><span class="p">:</span>
    <span class="sd">&#39;&#39;&#39;The Interface class provides to ability to identify specific class</span>
<span class="sd">    functions to be &#39;interface&#39; functions.</span>

<span class="sd">    This class contains a class property called InterfaceProperty which defines</span>
<span class="sd">    the name of the property used to identify interface functions.</span>

<span class="sd">    This class provides a :func:`interface` function which uses the</span>
<span class="sd">    InterfaceProperty property to create an implementation specific decorator</span>
<span class="sd">    which can be used to decorate functions that should identified as interface</span>
<span class="sd">    functions.</span>

<span class="sd">    Example::</span>

<span class="sd">       class Example(Interface):</span>
<span class="sd">           InterfaceProperty = &quot;ExampleInterfaceProperty&quot;</span>

<span class="sd">           @Example.interface</span>
<span class="sd">           def testPrint(self, message):</span>
<span class="sd">               print &quot;Message:&quot;, message</span>

<span class="sd">    This the above example the :func:`testPrint` function has been decorated</span>
<span class="sd">    and is thus an interface function.</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="nd">@classmethod</span>
<div class="viewcode-block" id="Interface.interface"><a class="viewcode-back" href="../../../../_generated/pyamp_patterns_interfaces_interface.html#pyamp.patterns.interfaces.interface.Interface.interface">[docs]</a>    <span class="k">def</span> <span class="nf">interface</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">function</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;A decorator for functions to make them interface functions for</span>
<span class="sd">        this Interface instance.</span>

<span class="sd">        * function -- The function to decorate</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="nb">setattr</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">cls</span><span class="o">.</span><span class="n">InterfaceProperty</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">function</span>
</div>
<div class="viewcode-block" id="Interface.getInterfaceFunctions"><a class="viewcode-back" href="../../../../_generated/pyamp_patterns_interfaces_interface.html#pyamp.patterns.interfaces.interface.Interface.getInterfaceFunctions">[docs]</a>    <span class="k">def</span> <span class="nf">getInterfaceFunctions</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Get a dictionary of the interface function names mapped</span>
<span class="sd">        to the actual functions.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">functions</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="c"># Traverse all of the class attributes</span>
        <span class="k">for</span> <span class="n">attrName</span> <span class="ow">in</span> <span class="nb">dir</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="n">attr</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attrName</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>

            <span class="c"># Only deal with existing functions</span>
            <span class="k">if</span> <span class="n">attr</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">attr</span><span class="p">,</span> <span class="s">&quot;__call__&quot;</span><span class="p">):</span>
                <span class="c"># Determine if the function is allowed to pass through</span>
                <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">attr</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">InterfaceProperty</span><span class="p">):</span>
                    <span class="n">functions</span><span class="p">[</span><span class="n">attrName</span><span class="p">]</span> <span class="o">=</span> <span class="n">attr</span>

        <span class="k">return</span> <span class="n">functions</span>
</div>
<div class="viewcode-block" id="Interface.connect"><a class="viewcode-back" href="../../../../_generated/pyamp_patterns_interfaces_interface.html#pyamp.patterns.interfaces.interface.Interface.connect">[docs]</a>    <span class="k">def</span> <span class="nf">connect</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Connect to a parent&#39;s interface methods.</span>

<span class="sd">        Note: This should be overriden by concrete Interfaces</span>

<span class="sd">        * parent -- The parent interface object</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">pass</span>
</div>
    <span class="nd">@classmethod</span>
<div class="viewcode-block" id="Interface.isInterface"><a class="viewcode-back" href="../../../../_generated/pyamp_patterns_interfaces_interface.html#pyamp.patterns.interfaces.interface.Interface.isInterface">[docs]</a>    <span class="k">def</span> <span class="nf">isInterface</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Determine if the given object is an instance of the interface.</span>

<span class="sd">        * obj -- The object</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">cls</span><span class="p">)</span>

</div></div>
<div class="viewcode-block" id="PassThrough"><a class="viewcode-back" href="../../../../_generated/pyamp_patterns_interfaces_interface.html#pyamp.patterns.interfaces.interface.PassThrough">[docs]</a><span class="k">class</span> <span class="nc">PassThrough</span><span class="p">(</span><span class="n">Interface</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The PassThrough :class:`Interface` provides the ability for a child</span>
<span class="sd">    PassThrough object to connect functions of the same name to the parent&#39;s</span>
<span class="sd">    :class:`Interface` functions.</span>

<span class="sd">    When a child class connects to the parent&#39;s interface functions, all calls</span>
<span class="sd">    to the child&#39;s interface functions are instead redirected to the parent&#39;s</span>
<span class="sd">    functions of the same name.</span>

<span class="sd">    Example::</span>

<span class="sd">        class Parent(PassThrough):</span>
<span class="sd">            @PassThrough.interface</span>
<span class="sd">            def testPrint(self, message):</span>
<span class="sd">                print &quot;Parent message:&quot;, message</span>

<span class="sd">        class Child(PassThrough):</span>
<span class="sd">            pass</span>

<span class="sd">        parent = Parent()</span>
<span class="sd">        child = Child()</span>
<span class="sd">        child.connect(parent)</span>

<span class="sd">        # Prints:</span>
<span class="sd">        #    &quot;Parent message:&quot;, this is a message</span>
<span class="sd">        child.testPrint(&quot;this is a message&quot;)</span>

<span class="sd">    &#39;&#39;&#39;</span>
    <span class="n">InterfaceProperty</span> <span class="o">=</span> <span class="s">&quot;PassThroughFunction&quot;</span>

<div class="viewcode-block" id="PassThrough.connect"><a class="viewcode-back" href="../../../../_generated/pyamp_patterns_interfaces_interface.html#pyamp.patterns.interfaces.interface.PassThrough.connect">[docs]</a>    <span class="k">def</span> <span class="nf">connect</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Connect the child to the parent&#39;s interface functions.</span>

<span class="sd">        * parent -- The parent Interface</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="c"># Make sure the parent implements the interface</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">PassThrough</span><span class="o">.</span><span class="n">isInterface</span><span class="p">(</span><span class="n">parent</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&quot;Parent [</span><span class="si">%s</span><span class="s">] must implement the &quot;</span> \
                                <span class="s">&quot;PassThrough interface!&quot;</span> <span class="o">%</span> <span class="n">parent</span><span class="p">)</span>

        <span class="c"># Duplicate each of our parent&#39;s interface functions for our</span>
        <span class="c"># own use, to pass the calls straight through</span>
        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">function</span> <span class="ow">in</span> <span class="n">parent</span><span class="o">.</span><span class="n">getInterfaceFunctions</span><span class="p">()</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">function</span><span class="p">)</span>

</div></div>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="k">class</span> <span class="nc">Parent</span><span class="p">(</span><span class="n">PassThrough</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Class for testing the pass through interface.&#39;&#39;&#39;</span>
        <span class="nd">@PassThrough.interface</span>
        <span class="k">def</span> <span class="nf">testPrint</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
            <span class="sd">&#39;&#39;&#39;Function for testing the pass through interface.&#39;&#39;&#39;</span>
            <span class="k">print</span> <span class="s">&quot;Parent message:&quot;</span><span class="p">,</span> <span class="n">message</span>

    <span class="k">class</span> <span class="nc">Child</span><span class="p">(</span><span class="n">PassThrough</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Class for testing the pass through interface.&#39;&#39;&#39;</span>
        <span class="k">pass</span>

    <span class="n">testParent</span> <span class="o">=</span> <span class="n">Parent</span><span class="p">()</span>
    <span class="n">testChild</span> <span class="o">=</span> <span class="n">Child</span><span class="p">()</span>
    <span class="n">testChild</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">testParent</span><span class="p">)</span>

    <span class="n">testChild</span><span class="o">.</span><span class="n">testPrint</span><span class="p">(</span><span class="s">&quot;this is a message&quot;</span><span class="p">)</span>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../../patterns.html" >pyamp.patterns</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Brett Ponsler.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
    </div>
  </body>
</html>